package com.loongtech.bi.action.advertisment;

import com.loongtech.bi.advertisment.controller.BaseAdvAction;
import com.loongtech.bi.advertisment.model.NewRemainData;
import com.loongtech.bi.advertisment.service.AdvDayDataAllService;
import com.loongtech.bi.manager.system.SysUserManager;
import com.loongtech.core.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: AdvDayAllDataAction
 *
 * @author liuchen
 * @Description: 市场数据分析 —— 广告日数据
 * @date 2017-03-28 下午2:23:49
 */
@Controller
@Scope("prototype")
@RequestMapping("/market/advDayDataRataPaidMarket")
public class AdvDayAllDataDetailBiAction extends BaseAdvAction {
    private static final long serialVersionUID = 1L;
    @Resource(name = "advDayDataAllService")
    private AdvDayDataAllService service;

    @Autowired
    private SysUserManager sysUserManager;

    @RequestMapping(value = "initParam.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void initParam(HttpServletRequest request, HttpServletResponse response, @RequestParam String uid, String callback) {
        try {
            Map<String, Object> result = new HashMap<>();
            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

            result.put("res", "0");
            result.put("surl", getId());
            result.put("affiliation", getAffiliation());
            result.put("channelName", getChannelName());
            result.put("agent", getAgent());
            result.put("platform", getPlatform());
            result.put("paytype", getPaytype());
            result.put("advPosition", getAdvPosition());
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String sday,
                       @RequestParam String eday, @RequestParam String agent, @RequestParam String platform,
                       @RequestParam String affiliation, @RequestParam String channelName, @RequestParam String paytype,
                       @RequestParam String advPosition, @RequestParam String uid, String callback) {
        try {
            Map<String, Object> result = new HashMap<>();
            Integer userid = sysUserManager.getUserIdByName(uid);
            if (null == userid) {
                result.put("res", 1);
                result.put("msg", "帐户错误，请联系管理员!");
                printToJson(response, result, callback);
                return;
            }
            initData(userid);

            if (hasAgents == null || hasAgents.size() <= 0) {
                result.put("res", "1");
                result.put("msg", "此用户无任何代理商权限，请联系管理员配置!");
                printToJson(response, result, callback);
                return;
            }

            String[] affiliations = "".equals(affiliation) ? getAffiliation().toArray(new String[getAffiliation().size()]) : Utils.matchesString(affiliation).split(",");
            String[] channelNames = "".equals(channelName) ? getChannelName().toArray(new String[getChannelName().size()]) : Utils.matchesString(channelName).split(",");
            String[] agents = "".equals(agent) ? getAgent().toArray(new String[getAgent().size()]) : Utils.matchesString(agent).split(",");
            String[] platforms = "".equals(platform) ? getPlatform().toArray(new String[getPlatform().size()]) : Utils.matchesString(platform).split(",");
            String[] paytypes = "".equals(paytype) ? getPaytype().toArray(new String[getPaytype().size()]) : Utils.matchesString(paytype).split(",");
            String[] advPositions = "".equals(advPosition) ? getAdvPosition().toArray(new String[getAdvPosition().size()]) : Utils.matchesString(advPosition).split(",");

            List<List<Object>> totalPayDatas = new ArrayList<>();
            List<List<String>> stotalPayDatas =
                    service.getTotalPayDatas(sday, eday,
                            affiliations, channelNames, agents,
                            platforms, paytypes, advPositions);
            for (List<String> ls : stotalPayDatas) {
                List<Object> lo = new ArrayList<>();
                for (int i = 0; i < ls.size(); i++) {
                    if (i < 1) {
                        lo.add(ls.get(i));
                    } else {
                        lo.add(Integer.valueOf(ls.get(i)));
                    }
                }
                totalPayDatas.add(lo);
            }

            List<NewRemainData> newRemainDatas = service.getNewRemainDatas(sday, eday,
                    affiliations, channelNames, agents,
                    platforms, paytypes, advPositions);

            result.put("res", "0");
            result.put("newRemainDatas", newRemainDatas);
            result.put("totalPayDatas", totalPayDatas);
            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            Map<String, Object> result = new HashMap<>();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }
}
